<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
        $this->registerRouteMacro();
    }

    /**
     * 路由管理.
     */
    public function registerRouteMacro()
    : void
    {
        if (!Route::hasMacro('description')) {
            Route::macro('description', function ($description) {
                $this->action['description'] = $description;

                return $this;
            });
        }

        if (!Route::hasMacro('getActionDescription')) {
            Route::macro('getActionDescription', function () {
                return $this->action['description'] ?? '';
            });
        }
        if (!Route::hasMacro('resourceDescription')) {
            Route::macro('resourceDescription', function ($name, $contoller, $description) {                
                $names = Str::plural($name);
                Route::get("/$names", "$contoller@index")->name("$names.index")->description("$description:列表");
                Route::post("/$names", "$contoller@store")->name("$names.store")->description("$description:新增");
                Route::get("/$names/{$name}", "$contoller@show")->name("$names.show")->description("$description:详情");
                Route::put("/$names/{$name}", "$contoller@update")->name("$names.update")->description("$description:修改");
                Route::delete("/$names/{$name}", "$contoller@destroy")->name("$names.destroy")->description("$description:删除");
            });
        }
    }

}
